using UnityEngine;
using System.Collections;

public class GroundManager : MonoBehaviour {
	
	public static GroundManager Instance {
		get;
		private set;
	}
	
	public void Awake(){
		Instance = this;
	}
	
	
	//public ShopHUD _ShopHUD;
	public Texture2D [] _shopTextures;
	public ItemManager _itemManager;
	public Transform prefab;
	public Transform shopprefab;
	public Transform _moneyHUDPrefab;
	public Transform stairs;
	//public Transform playerprefab;//stuff moved to PlayerManager
	public Material[] shopMats;
	//public Material[] playerMats;
	public Vector3[] floorSizes;
	public Vector3[] floorPositions;
	
	private Transform[] shops;
	private int numberOfShops;
	
	//private Transform spawnedPlayer;
	
	private Transform[] roomObjects;
	private Transform[] stairObjects;

	public Vector3 position;
	public float shopX, shopZ;
	
	
	
	
	// Use this for initialization
	void Start () 
	{
		
		makefloors();
		makeshops();
		
		//Instantiate(_moneyHUDPrefab);
		
		/*Moved player spawning to player manager
		spawnedPlayer = (Transform)Instantiate (playerprefab);
		spawnedPlayer.transform.FindChild("guy8bones").FindChild("Guybody").renderer.material = playerMats[1];
					//go through scene hierachy to find the renderer of player paint him
		Player thisPlayer = spawnedPlayer.transform.GetComponent<Player>();
		
		//thisPlayer.SetKeys(KeyCode.W,KeyCode.A,KeyCode.D); //this can't work
		
		thisPlayer.SetItemMan(_itemManager);//hands stuff to the player
		*/
		
		
		//_ShopHUD._player = thisPlayer;
		
	}
	
	private void makefloors ()
	{
		floorSizes = new Vector3[3];
		floorPositions = new Vector3[3];
		roomObjects = new Transform[3];
		
		floorSizes[0].x = (float)Random.Range(6,12);//sizes for corridor (room1)
		floorSizes[0].y = 0.2f;
		floorSizes[0].z = (float)Random.Range(3,7);
		
		roomObjects[0] = (Transform)Instantiate(prefab);
		roomObjects[0].localScale = floorSizes[0];
		
		floorSizes[1].x = (float)Random.Range(4,8);//sizes for left room (room2)
		floorSizes[1].y = 1.2f;
		floorSizes[1].z = (float)Random.Range(4,6);
		
		floorPositions[1].x = -(floorSizes[1].x/2)-(floorSizes[0].x/2);
		floorPositions[1].x += 2f + (float)Random.Range (0,(int)(floorSizes[0].x/2-2));//random offset with room alignments
		floorPositions[1].y = floorSizes[1].y/2-floorSizes[0].y/2;//flush bases
		floorPositions[1].z = (floorSizes[1].z/2)+(floorSizes[0].z/2);
		
		roomObjects[1] = (Transform)Instantiate(prefab);
		roomObjects[1].localScale = floorSizes[1];
		roomObjects[1].localPosition = floorPositions[1];
		
		floorSizes[2].x = (float)Random.Range(4,8);//sizes for left room (room3)
		floorSizes[2].y = 1.2f;
		floorSizes[2].z = (float)Random.Range(4,6);
		
		floorPositions[2].x = +(floorSizes[2].x/2)+(floorSizes[0].x/2);
		floorPositions[2].x -= 2f + (float)Random.Range (0,(int)(floorSizes[0].x/2-2));//random offset with room alignments
		floorPositions[2].y = floorSizes[2].y/2-floorSizes[0].y/2;//flush bases
		floorPositions[2].z = (floorSizes[2].z/2)+(floorSizes[0].z/2);
		
		roomObjects[2] = (Transform)Instantiate(prefab);
		roomObjects[2].localScale = floorSizes[2];
		roomObjects[2].localPosition = floorPositions[2];
		
		stairObjects = new Transform[2];
		stairObjects[0] = (Transform)Instantiate (stairs);
		Vector3	xmove = new Vector3();
		xmove =stairObjects[0].localPosition;
		xmove.x =floorPositions[2].x-(floorSizes[2].x/2)+1;
		xmove.z = floorPositions[0].z+(floorSizes[0].z/2)-1.1f;
		stairObjects[0].localPosition = xmove;
		
		stairObjects[1] = (Transform)Instantiate (stairs);
		xmove =stairObjects[1].localPosition;
		xmove.x =floorPositions[1].x+(floorSizes[1].x/2)-1;
		xmove.z = floorPositions[0].z+(floorSizes[0].z/2)-1.1f;
		stairObjects[1].localPosition = xmove;
		
	}
	
	private void makeshops ()
	{
		numberOfShops = (int)((floorSizes[0].x)+(floorSizes[0].z*2) 
							+(floorSizes[1].x)+(floorSizes[1].z*2)
							+(floorSizes[2].x)+(floorSizes[2].z*2)
							
							-(floorPositions[1].x-(floorSizes[1].x/2)+(floorSizes[0].x/2))//positions 1 is negative // these are for the overlapped bits
							
							+(floorPositions[2].x+(floorSizes[2].x/2)-(floorSizes[0].x/2))
					
							+(-floorPositions[1].x-(floorSizes[1].x/2))+(floorPositions[2].x-(floorSizes[2].x/2) ));//space after offsets
		
		shops = new Transform[numberOfShops];
		
		for(int i=0;i<numberOfShops;i++)//initialise all the shops
			
		{
			
			shops[i] = (Transform) Instantiate(shopprefab);
			Shop newShopScript = (Shop) shops[i].GetComponent<Shop>();
			//newShopScript.SetUpShop(_itemManager, this, _ShopHUD);
			
			
			int materialIndex = Random.Range (0, shopMats.Length);
			shops[i].renderer.material = shopMats[materialIndex];
			
			if ((materialIndex == 5) ||(materialIndex == 6)||(materialIndex == 7))
			{
				shops[i].collider.isTrigger = false;
		}
			
		
		}
		
		int c = 0;
		
		
		Vector3 shopPlacer = new Vector3();
		float placerheight = 0.5f;
		
		shopPlacer = floorPositions[1]+(floorSizes[1]/2);//room2 top row
		shopPlacer.y += placerheight;
		
		for(int i=0;i<floorSizes[1].x;i++)//this relies on shops being width 1!!! using increment i
		{
			Vector3 t = new Vector3(shopPlacer.x-i-0.5f,shopPlacer.y,shopPlacer.z+0.25f);//0.5 is half shop width
			shops[c].localPosition = t;
			c++;
			//maybe should add shops to a local room structure or something
		}
		
		shopPlacer = floorPositions[2]+(floorSizes[2]/2);//room3 top row
		shopPlacer.y += placerheight;
		
		for(int i=0;i<floorSizes[2].x;i++)//this relies on shops being width 1!!! using increment i
		{
			Vector3 t = new Vector3(shopPlacer.x-i-0.5f,shopPlacer.y,shopPlacer.z+0.25f);//0.5 is half shop width 0.25f heigh extra 
			shops[c].localPosition = t;
			c++;
			//should add shops to a local room structure or something
		}
		
		placerheight = -0.5f;	//middle row room1 piece
		
		shopPlacer.x =floorPositions[1].x+floorSizes[1].x/2;
		shopPlacer.y = floorPositions[1].y+floorSizes[1].y/2 + placerheight;
		shopPlacer.z = floorPositions[1].z-floorSizes[1].z/2;	
		
		
		for (int i=0;shopPlacer.x+i <floorPositions[2].x-floorSizes[2].x/2;i++)
		{
			Vector3 t = new Vector3(shopPlacer.x+i+0.5f,shopPlacer.y,shopPlacer.z+0.25f);//0.5 is half shop width	
			shops[c].localPosition = t;
			c++;
		}
		
		shopPlacer = floorPositions[1]-(floorSizes[1]/2);//room2 left wall
		placerheight = 0.5f;
		shopPlacer.y = floorPositions[1].y+floorSizes[1].y/2+placerheight;
		
		for(int i=0;i<floorSizes[1].z;i++)//this relies on shops being width 1!!! using increment i
		{
			Vector3 t = new Vector3(shopPlacer.x-0.25f,shopPlacer.y,shopPlacer.z+i+0.5f);//0.5 is half shop width 0.25f heigh extra 
			shops[c].localPosition = t;
			shops[c].RotateAround(new Vector3(0f,1f,0f),-Mathf.PI/2);
			
			c++;
		}
		
		placerheight = 0.5f;
		shopPlacer.x = floorPositions[1].x +(floorSizes[1].x/2);//room2 right wall
		shopPlacer.y = floorPositions[1].y+floorSizes[1].y/2+placerheight;
		shopPlacer.z = floorPositions[1].z+(floorSizes[1].z/2);
		
		for(int i=0;i<floorSizes[1].z;i++)//this relies on shops being width 1!!! using increment i
		{
			Vector3 t = new Vector3(shopPlacer.x+0.25f,shopPlacer.y,shopPlacer.z-i-0.5f);//0.5 is half shop width 0.25f heigh extra 
			shops[c].localPosition = t;
			shops[c].RotateAround(new Vector3(0f,1f,0f),-Mathf.PI*3/2);
			c++;
		}
		
		shopPlacer = floorPositions[2]-(floorSizes[2]/2);//room3 left wall
		placerheight = 0.5f;
		shopPlacer.y = floorPositions[2].y+floorSizes[2].y/2+placerheight;
		
		for(int i=0;i<floorSizes[2].z;i++)//this relies on shops being width 1!!! using increment i
		{
			Vector3 t = new Vector3(shopPlacer.x-0.25f,shopPlacer.y,shopPlacer.z+i+0.5f);//0.5 is half shop width 0.25f heigh extra 
			shops[c].localPosition = t;
			shops[c].RotateAround(new Vector3(0f,1f,0f),-Mathf.PI/2);
			c++;
		}
		
		shopPlacer = floorPositions[2]+(floorSizes[2]/2);//room3 right wall
		placerheight = 0.5f;
		shopPlacer.y += placerheight;
				
		for(int i=0;i<floorSizes[2].z;i++)//this relies on shops being width 1!!! using increment i
		{
			Vector3 t = new Vector3(shopPlacer.x+0.25f,shopPlacer.y,shopPlacer.z-i-0.5f);//0.5 is half shop width 0.25f heigh extra 
			shops[c].localPosition = t;
			shops[c].RotateAround(new Vector3(0f,1f,0f),-Mathf.PI*3/2);
			c++;
		}
		
		shopPlacer = floorPositions[0]-(floorSizes[0]/2);//Room1 left wall
		placerheight=0.5f;
		shopPlacer.y = floorPositions[0].y+(floorSizes[0].y/2)+ placerheight;
		
		for(int i=0;i<floorSizes[0].z;i++)//this relies on shops being width 1!!! using increment i
		{
			Vector3 t = new Vector3(shopPlacer.x-0.25f,shopPlacer.y,shopPlacer.z+i+0.5f);//0.5 is half shop width 0.25f heigh extra 
			shops[c].localPosition = t;
			shops[c].RotateAround(new Vector3(0f,1f,0f),-Mathf.PI/2);
			c++;
		}	
		
		
		shopPlacer = floorPositions[0]+(floorSizes[0]/2);//Room1 right wall
		
		shopPlacer.y += placerheight;
		
		for(int i=0;i<floorSizes[0].z;i++)//this relies on shops being width 1!!! using increment i
		{
			Vector3 t = new Vector3(shopPlacer.x+0.25f,shopPlacer.y,shopPlacer.z-i-0.5f);//0.5 is half shop width 0.25f heigh extra 
			shops[c].localPosition = t;
			shops[c].RotateAround(new Vector3(0f,1f,0f),-Mathf.PI*3/2);
			c++;
		}	
		
		shopPlacer = floorPositions[0]-(floorSizes[0]/2);//Room1 bottom wall
		
		shopPlacer.y = floorPositions[0].y+(floorSizes[0].y/2)+ placerheight;
		
		for(int i=0;i<floorSizes[0].x;i++)//this relies on shops being width 1!!! using increment i
		{
			Vector3 t = new Vector3(shopPlacer.x+i+0.5f,shopPlacer.y,shopPlacer.z-0.25f);//0.5 is half shop width 0.25f heigh extra 
			shops[c].localPosition = t;
			shops[c].RotateAround(new Vector3(0f,1f,0f),-Mathf.PI);
			c++;
		}	
		
		shopPlacer = floorPositions[1]-(floorSizes[1]/2);//Room 2 bottom wall piece
		placerheight = 0.5f;
		shopPlacer.y = floorPositions[1].y+(floorSizes[1].y/2) + placerheight;
		
		for(int i=0;shopPlacer.x+i<(-floorSizes[0].x/2);i++)//this relies on shops being width 1!!! using increment i
		{
			Vector3 t = new Vector3(shopPlacer.x+i+0.5f,shopPlacer.y,shopPlacer.z-0.25f);//0.5 is half shop width 0.25f heigh extra 
			shops[c].localPosition = t;
			shops[c].RotateAround(new Vector3(0f,1f,0f),-Mathf.PI);
			c++;
		}	
		
		shopPlacer.x = floorPositions[2].x+(floorSizes[2].x/2);//Room 3 bottom wall piece
		placerheight = 0.5f;
		shopPlacer.y = floorPositions[2].y+(floorSizes[2].y/2) + placerheight;
		shopPlacer.z = floorPositions[2].z-(floorSizes[2].z/2);
		
				
		for(int i=0;shopPlacer.x-i>(floorSizes[0].x/2);i++)//this relies on shops being width 1!!! using increment i
		{
			Vector3 t = new Vector3(shopPlacer.x-i-0.5f,shopPlacer.y,shopPlacer.z-0.25f);//0.5 is half shop width 0.25f heigh extra 
			shops[c].localPosition = t;
			shops[c].RotateAround(new Vector3(0f,1f,0f),-Mathf.PI);
			c++;
		}	
		
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
	
	public Texture2D GetRandomImage(){
		int rand = Random.Range(0, _shopTextures.Length);
		return _shopTextures[rand];
	}
	
}
